import 'package:signage/app/data/models/member_group_model.dart';
import 'package:signage/app/data/models/template_model.dart';
import 'package:signage/app/modules/device/models/device_cached_info.dart';

class CastRecord {
  final DateTime timestamp;
  final bool success;
  final String meetingTitle;
  final int castMode;
  final String? id;
  // 模板信息
  final TemplateModel? templateA;
  final TemplateModel? templateB;

  // 设备信息
  final List<DeviceCachedInfo> deviceInfos;

  // 名单信息
  final List<MemberItem> memberInfos;

  // 是否完成
  final bool isPinned;

  // 组信息
  final int groupId;
  final String groupName;

  final int successCount;
  final int failureCount;
  CastRecord({
    required this.timestamp,
    required this.success,
    required this.meetingTitle,
    required this.castMode,
    required this.successCount,
    required this.failureCount,
    this.id,
    this.templateA,
    this.templateB,
    required this.deviceInfos,
    required this.memberInfos,
    this.isPinned = false,
    required this.groupId,
    required this.groupName,
  });

  Map<String, dynamic> toJson() {
    return {
      'timestamp': timestamp.toIso8601String(),
      'success': success,
      'meetingTitle': meetingTitle,
      'castMode': castMode,
      'id': id,
      'templateA': templateA?.toJson(),
      'templateB': templateB?.toJson(),
      'deviceInfos': deviceInfos.map((e) => e.toJson()).toList(),
      'memberInfos': memberInfos.map((e) => e.toJson()).toList(),
      'successCount': successCount,
      'failureCount': failureCount,
      'isPinned': isPinned,
      'groupId': groupId,
      'groupName': groupName,
    };
  }

  factory CastRecord.fromJson(Map<String, dynamic> json) {
    return CastRecord(
      timestamp: DateTime.parse(json['timestamp']),
      success: json['success'],
      meetingTitle: json['meetingTitle'],
      castMode: json['castMode'],
      id: json['id'],
      templateA: json['templateA'] != null
          ? TemplateModel.fromJson(json['templateA'])
          : null,
      templateB: json['templateB'] != null
          ? TemplateModel.fromJson(json['templateB'])
          : null,
      deviceInfos: (json['deviceInfos'] as List)
          .map((e) => DeviceCachedInfo.fromJson(e))
          .toList(),
      memberInfos: (json['memberInfos'] as List)
          .map((e) => MemberItem.fromJson(e))
          .toList(),
      isPinned: json['isPinned'],
      groupId: json['groupId'],
      groupName: json['groupName'],
      successCount: json['successCount'],
      failureCount: json['failureCount'],
    );
  }

  CastRecord copyWith({
    DateTime? timestamp,
    bool? success,
    String? meetingTitle,
    int? castMode,
    TemplateModel? templateA,
    TemplateModel? templateB,
    List<DeviceCachedInfo>? deviceInfos,
    List<MemberItem>? memberInfos,
    bool? isPinned,
    int? groupId,
    String? groupName,
  }) {
    return CastRecord(
        timestamp: timestamp ?? this.timestamp,
        success: success ?? this.success,
        meetingTitle: meetingTitle ?? this.meetingTitle,
        castMode: castMode ?? this.castMode,
        templateA: templateA ?? this.templateA,
        templateB: templateB ?? this.templateB,
        deviceInfos: deviceInfos ?? this.deviceInfos,
        memberInfos: memberInfos ?? this.memberInfos,
        isPinned: isPinned ?? this.isPinned,
        groupId: groupId ?? this.groupId,
        id: id ?? this.id,
        groupName: groupName ?? this.groupName,
        successCount: this.successCount,
        failureCount: this.failureCount);
  }
}
